Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve 2022 day 02 #13

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open

Solve 2022 day 02 #13

wants to merge 34 commits into from

Conversation

katzuv
Copy link
Owner

@katzuv katzuv commented Dec 9, 2022

No description provided.

@katzuv katzuv added the solution Puzzle solution label Dec 9, 2022
@katzuv katzuv self-assigned this Dec 9, 2022
@katzuv katzuv marked this pull request as ready for review December 10, 2022 00:05
@katzuv katzuv requested a review from YoniKF December 10, 2022 08:52
Copy link
Collaborator

@YoniKF YoniKF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented on part 1, where do I see the problem definition for part 2?

puzzles/solutions/2022/d02/p1.py Outdated Show resolved Hide resolved
def get_moves(input_text: str) -> list[tuple[str, str]]:
"""
:param input_text: puzzle input
:return: list of (opponent move, our move) couples
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the term "pair" instead of "couple".

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 583c923.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the same for all other places "couple" is used.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did I miss any in that day's solutions?

OPPONENT_PAPER = "B"
OPPONENT_SCISSORS = "C"

OUR_TO_OPPONENT_SHAPE_DEFEAT = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The different dictionaries here have different logical levels of abstractions.
The first two tell you what is the defeating shape, i.e., it is in the level of the general rock-paper-scissors game.
The third one tells you what input characters are the same shape, i.e., it is in the level of the definition of the problem's input -- not actually related to the game.
The fourth one tells you what the score is for each shape, i.e., it is in the level of the rock-paper-scissors tournament -- one level above the general game. Additional inconsistency is that some scores are defined here as a constant, and others are defined in the code itself.

I suggest you separate the levels of abstraction more clearly:

  1. First, parse the problem input into some object describing the rock-paper-scissors game. Note that you don't really need to continue with the "our" and "opponent" shape distinction: If you represent a turn as a pair of shapes (our, opponent), you can use the same object for "our" and for "opponent" rock.
  2. Define a function which reports the result of the turn, at the level of the rock-paper-scissors game -- So nothing about scores yet. This function can be a dictionary with all possible turns as keys are the corresponding values representing the result in some way.
  3. Define a function which reports the score of the turn, at the level of the rock-paper-scissors tournament, based on the turn pair (from step 1) and the turn's result (from step 2).

@katzuv
Copy link
Owner Author

katzuv commented Dec 10, 2022

Commented on part 1, where do I see the problem definition for part 2?

It unlocks after solving part 1, so I'll paste it here:

The Elf finishes helping with the tent and sneaks back over to you. "Anyway, the second column says how the round needs to end: X means you need to lose, Y means you need to end the round in a draw, and Z means you need to win. Good luck!"

The total score is still calculated in the same way, but now you need to figure out what shape to choose so the round ends as indicated. The example above now goes like this:

  • In the first round, your opponent will choose Rock (A), and you need the round to end in a draw (Y), so you also choose Rock. This gives you a score of 1 + 3 = 4.
  • In the second round, your opponent will choose Paper (B), and you choose Rock so you lose (X) with a score of 1 + 0 = 1.
  • In the third round, you will defeat your opponent's Scissors with Rock for a score of 1 + 6 = 7.

Now that you're correctly decrypting the ultra top secret strategy guide, you would get a total score of 12.

Following the Elf's instructions for the second column, what would your total score be if everything goes exactly according to your strategy guide?

import shapes


DEFEAT = "X"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda lousy that you have a shapes modules for constants, where you define OUR_ROCK = "X", but also define DEFEAT = "X" here.

if outcome == DEFEAT:
return shapes.OPPONENT_TO_OUR_SHAPE_DEFEAT[opponent_move]
if outcome == DRAW:
for our_shape, opponent_shape in shapes.IDENTICAL_SHAPES.items():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This for-loop means that you should have an inverted version of the IDENTICAL_SHAPES dictionary.

@katzuv
Copy link
Owner Author

katzuv commented Dec 10, 2022

I made quite a few changes, see what you think now.
About

This for-loop means that you should have an inverted version of the IDENTICAL_SHAPES dictionary.

I decided to leave a for-loop in choose_shape so to not have three different inverted dictionaries.

@katzuv katzuv force-pushed the solve/2022-02 branch 3 times, most recently from 74812ad to 768d850 Compare December 10, 2022 20:06
This module contains now more constants than just shapes, so a name change is desired.
@katzuv katzuv requested a review from YoniKF December 23, 2022 15:06
@YoniKF YoniKF removed their request for review February 25, 2024 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution Puzzle solution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants